<template>
  <div class="userWithdraw">
    <h3>用户提现</h3>
    <div class="search">
      <div class="cond1 cond">
        <el-input placeholder="请输入用户名" v-model="search1" clearable>
          <template slot="prepend">用户名</template>
        </el-input>
      </div>
      <div class="cond1 cond">
        <el-input placeholder="请输入用户id" v-model="search2" clearable>
          <template slot="prepend">用户ID</template>
        </el-input>
      </div>
      <div class="cond1 cond">
        <el-input placeholder="请输入提现单号" v-model="search3" clearable>
          <template slot="prepend">提现单号</template>
        </el-input>
      </div>
      <div class="cond2 cond">
        <label>提现状态</label>
        <el-select v-model="search4" placeholder="请选择" clearable>
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div>
      <div>
        <el-button type="primary" icon="el-icon-search" @click="initDataList({currentPage: 1})">搜索</el-button>
      </div>
    </div>
    <div class="list">
      <el-table
        border
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
      >
        <el-table-column
          header-align="center"
          align="center"
          prop="id"
          label="序号"
          sortable
          width="90"
        ></el-table-column>
        <el-table-column header-align="center" align="center" prop="sn" label="提现单号"></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="user.nick"
          label="用户名"
          width="120"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="user.code"
          label="用户ID"
          width="100"
        ></el-table-column>
        <el-table-column header-align="center" align="center" prop="money" label="提现金额" width="100"></el-table-column>
        <el-table-column header-align="center" align="center" prop="fee" label="费率%" width="100">
          <template slot-scope="scope">{{ scope.row.fee }}%</template>
        </el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="true_money"
          label="实际金额"
          width="100"
        ></el-table-column>
        <el-table-column header-align="center" align="center" prop="type" label="提现类型" width="100">
          <template slot-scope="scope">{{ scope.row.type | filterType }}</template>
        </el-table-column>
        <el-table-column header-align="center" align="center" prop="name" label="账户名" width="150"></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="bank_name"
          label="银行名称"
          width="120"
        ></el-table-column>
        <el-table-column header-align="center" align="center" prop="no" label="提现账户" width="100"></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="updated_at"
          label="申请/处理时间"
          width="150"
        ></el-table-column>
        <el-table-column header-align="center" align="center" prop="remark" label="备注" width="200"></el-table-column>
        <el-table-column header-align="center" align="center" label="操作" width="100" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="warning"
              size="mini"
              @click="handleEdit(scope.row)"
              v-if="permissions_obj.id_196||permissions_obj.id_all"
              v-show="scope.row.status==0"
            >待处理</el-button>
            <el-button
              type="success"
              size="mini"
              v-show="scope.row.status==1"
            >&nbsp;&nbsp;成功&nbsp;&nbsp;</el-button>
            <el-button
              type="danger"
              size="mini"
              v-show="scope.row.status==2"
            >&nbsp;&nbsp;失败&nbsp;&nbsp;</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="page">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-size="pageSize"
        layout="total, prev, pager, next, jumper"
        :total="totalSize"
      ></el-pagination>
    </div>
    <!-- 编辑弹窗 -->
    <el-dialog title="处理提现" :visible.sync="dialogVisible" width="30%" center class="dialog">
      <el-form ref="dialogForm" :model="dialogForm" label-width="100px">
        <el-form-item label="备注：">
          <el-input type="textarea" placeholder="请输入备注" v-model="dialogForm.remark" clearable></el-input>
        </el-form-item>
        <div class="dialogBtn">
          <el-button type="primary" size="small" @click="submitForm()">成功</el-button>
          <el-button type="info" size="small" @click="cancelForm()">失败</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "userWithdraw",
  data() {
    return {
      /**
       * 自定义数据
       * @param {array} [options] - 状态
       */
      options: [
        {
          value: "",
          label: "全部"
        },
        {
          value: 0,
          label: "处理中"
        },
        {
          value: 1,
          label: "成功"
        },
        {
          value: 2,
          label: "失败"
        }
      ],
      /**
       * 查询条件
       * @param {string} [search1] - 用户名
       * @param {string} [search2] - 用户id
       * @param {string} [search3] - 用户提现单号
       * @param {string} [search4] - 提现状态
       */
      search1: undefined,
      search2: undefined,
      search3: undefined,
      search4: undefined,
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      totalSize: 0,
      /**
       * 编辑弹出框
       * @param {boolean} [dialogVisible]     - 是否显示编辑弹出框
       * @param {boolean} [dialogForm]        - 编辑弹出框表单
       * @param {number}  [dialogForm.remark] - 编辑弹出框表单-标签排序
       * @param {object}  [dialogRule]        - 编辑弹出框表单验证规则
       */
      dialogVisible: false,
      dialogForm: {
        remark: undefined
      },
      selectId:"",
      flag:true,
      // 权限管理
      permissions: [],
      permissions_obj: {}
    };
  },
  filters: {
    /**
     * 提现类型
     * @option {number} [1] - 支付宝
     * @option {number} [2] - 银行卡
     */
    filterType(value) {
      switch (value) {
        case 1:
          return "支付宝";
        case 2:
          return "银行卡";
        default:
          return "未知";
      }
    }
  },
  created() {
    //权限判断
    this.permissions = this.$store.state.permissions;
    let oneOff = false;
    this.permissions.forEach((item, index) => {
      if (item == -1) {
        oneOff = true;
      } else {
        this.permissions_obj["id_" + item] = item;
      }
    });
    if (oneOff) {
      this.permissions_obj.id_all = true;
    } else {
      this.permissions_obj.id_all = false;
    }
  },
  mounted() {
    /**
     * 挂载
     * @function [initDataList] - 初始化列表
     */
    this.initDataList();
  },
  methods: {
    /**
     * 初始化列表
     * @param {object} [data]    - 调用时传递的参数【主要是用来判定如果是搜索按钮时，需要把page设为1】
     * @param {object} [parames] - 列表后端需要的参数
     */
    async initDataList(data) {
      // 判定是否含有 page【有currentPage字段就把data里的currentPage设为1】
      if (data && data.currentPage) {
        this.currentPage = 1;
      }
      let parames = {
        handshake: "",
        data: {
          page: String(this.currentPage),
          page_size: String(this.pageSize),
          search: {
            nick: this.search1,
            code: this.search2,
            sn: this.search3,
            status: this.search4
          }
        }
      };
      let res = await this.$axios.post(
        this.$api.withdraw_list,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.tableData = res.data.data;
        this.totalSize = res.data.total;
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
  
    /**
     * 【按钮】待处理按钮事件
     * @param {object} [row] - 编辑按钮所在当前行数据
     * @param {boolean} [dialogVisible]     - 是否显示弹出框
     * @param {object}  [dialogForm]        - 弹出框表单
     * @param {object}  [dialogForm.remark] - 弹出框表单-备注
     */
    handleEdit(row) {
      this.dialogVisible = true;
      this.dialogForm.remark = row.remark;
      this.selectId=row.id;
    },
    /**
     * 【分页】切换每页显示的条数
     * @param {number} [val]         - 当前选中的数值
     * @param {number} [currentPage] - 当前选中的页码
     * @param {number} [pageSize]    - 每页显示的条数
     */
    handleSizeChange(val) {
      this.currentPage = 1;
      this.pageSize = val;
      this.initDataList();
    },
    /**
     * 【分页】切换页码
     * @param {number} [val]         - 当前选中的数值
     * @param {number} [currentPage] - 当前选中的页码
     */
    handleCurrentChange(val) {
      this.currentPage = val;
      this.initDataList();
    },
    /**
     * 【编辑弹出框】初始化表单验证+初始化表单数据
     */
    initDialog() {
      this.$refs.dialogForm.clearValidate();
    },
    /**
     * 【编辑弹出框】提交
     * @param {string} [formName] - 弹出框表单名称
     * @function [initDataList] - 初始化列表
     */
    submitForm() {
      this.submitMethod(1);
    },
    cancelForm() {
      this.submitMethod(2);
    },
    async submitMethod(state) {
      if(!this.flag){
        return;
      }
      this.flag=false;
      let parames = {
        handshake: "",
        data: {
            id: this.selectId,
            status: state,
            remark: this.dialogForm.remark,
        }
      };
      let res = await this.$axios.post(
        this.$api.withdraw_update,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
       this.$message({
          message: res.message,
          type: "success"
        });
        this.dialogVisible=false;
        this.flag=true;
        this.initDataList();
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
        this.flag=true;
      }
    },
    /**
     * 【编辑弹出框】取消提交
     * @function [initDialog] - 初始化弹出框表单方法
     * @param {boolean} [dialogVisible] - 是否显示弹出框
     */
    cancelForm() {
      this.submitMethod(2);
    }
  }
};
</script>
<style scoped>
h3 {
  padding: 0.2rem;
  color: #3c763d;
}
.search {
  display: flex;
  margin-top: 0.2rem;
  padding-left: 0.2rem;
  width: 100%;
  flex-wrap: wrap;
}
label {
  width: 0.83rem;
  height: 0.4rem;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f5f7fa;
  color: #909399;
  border: 1px solid #dcdfe6;
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
  font-size: 14px;
}
.cond {
  display: flex;
  margin-right: 0.4rem;
  margin-bottom: 0.1rem;
}
.search .el-select >>> .el-input__inner {
  border-left: none;
  border-top-left-radius: 0px;
  border-bottom-left-radius: 0px;
}
.search .el-select {
  width: 1.67rem;
}
.opration {
  margin-top: 0.2rem;
  padding-left: 0.2rem;
}
.page {
  margin-top: 0.2rem;
  display: flex;
  justify-content: center;
}
.list {
  margin-top: 0.2rem;
  padding: 0 0.1rem;
  font-size: 0.12rem;
}
.dialogBtn {
  display: flex;
  justify-content: center;
  margin-top: 0.3rem;
}
</style>